package com.lg.template.mapper.plus;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lg.template.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * mybatis 中有的 plus都可以实现
 * <p>
 * 一对多  多对一
 * 等等各种其他的用法， 见到了在补充
 * 不用问到底什么是最优 什么是最终的解决方法，你知道什么最优就用什么
 * 不断的优化不断的补充就行了
 */
public interface PlusUser extends BaseMapper<User> {

    //名称与数据库不一致
    @Results(id = "defaultRes", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "password", column = "pwd")
    })
    @Select("select * from user")
    List<User> selectByAnnotation();

    @ResultMap("defaultRes")
    @Select("select * from user where id =7")
    User selectOneByAnnotation();

    //拼接sql
    @Update({"<script>",
            "update user set",
            "<if test='name != null and name != \"\"'>",
            "name = #{name} ,",
            "</if>",
            "id =#{id}",
            "where id = #{id}",
            "</script>"})
    Integer updateByAnnotation(User user);


    @Insert("insert into user(id,name) values(#{id},#{name})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insertByAnnotation(User user);


    @Delete("delete from user where id = #{id}")
    Integer deleteByAnnotation(Integer id);
}
